<?php
/**
 * Created by PhpStorm.
 * User: qingyun
 * Date: 17/4/12
 * Time: 下午7:29
 */
function _md5($data){
    return md5(SALT . $data . SALT);
}
function getTree($data , $pid = 0 , $level = 0 , $html = '&nbsp;'){
    $arr = [];
    foreach($data as $v){
        if($v['pid'] == $pid){
            $v['level'] = $level + 1;
            $v['html'] = str_repeat($html,$level);
            $arr[] = $v;
            $arr = array_merge($arr,getTree($data,$v['id'],$level + 1,$html));
        }
    }
    return $arr;
}
function make_tree($list,$root=0,$pk='id',$pid='pid',$child='_child'){
    $tree=array();
    $packData=array();
    foreach ($list as $data) {
        $packData[$data[$pk]] = $data;
    }
    foreach ($packData as $key =>$val){
        if($val[$pid]==$root){//代表跟节点
            $tree[]=& $packData[$key];
        }else{
            //找到其父类
            $packData[$val[$pid]][$child][]=& $packData[$key];
        }
    }
    return $tree;
}
function imgOper($name,$path = 'Upload/'){
    $fileData = $_FILES[$name];
    if($fileData['size'] == 0){
        return ['msg' => '图片上传失败！','status' => '0'];
    }
//    if($fileData['size'] > 204800){
//        return ['msg' => '图片太大！','status' => '0'];
//    }
    $datatype = ["image/png","image/jpg","image/jpeg","image/gif"];
    if(!in_array($fileData['type'],$datatype)){
        return ['msg' => '上传图片类型错误!','status' => '0'];
    }
    $mtime=explode(' ',microtime());
    $startTime=$mtime[1].$mtime[0]*10000000;
    $fileName = $startTime .'.'.end(explode('.',$fileData['name']));
    $filePath = $path;
    if(!is_dir($filePath) && !is_writable($filePath)){
        return ['msg' => '文件夹存储异常！','status' => 0];
    }
    $litPath = $filePath.$fileName;
    if(!move_uploaded_file($fileData['tmp_name'],$litPath)){
        return [ 'msg' => '头像上传失败！','status' => 0];
    }
    return $litPath;
}
function uploadFile($fileDATA,$filePath = 'Upload/head/'){
    if(empty($fileDATA)){
        return ['status' => 1, 'msg' => '没有上传图片！'];
    }
    $typeArr = ['image/jpeg','image/png','image/jpg','image/gif'];
    if(!in_array($fileDATA['type'],$typeArr)){
        return ['status' => 1, 'msg' => '缩略图不符合要求！'];
    }
    if($fileDATA['size'] > 204800){
        return ['status' => 1, 'msg' => '缩略图太大！'];
    }
    $fileName = time().'.'.end(explode('.',$fileDATA['name']));
    if(!is_dir($filePath) && !is_writable($filePath)){
        return ['status' => 1, 'msg' => '文件夹存储异常！'];
    }
    $litPath = $filePath.$fileName;
    if(!move_uploaded_file($fileDATA['tmp_name'],$litPath)){
        return ['status' => 1, 'msg' => '缩略图上传失败！'];
    }
    return $litPath;
}